<?php

namespace apps\marriage\app\op\controller;

use apps\marriage\core\service\UserService;
use apps\marriage\core\service\UserWithdrawAccountService;
use apps\marriage\core\service\UserWithdrawCashService;
use think\Request;

class WithdrawCashController extends BaseMarriageOpController
{
    /**
     * @var UserWithdrawCashService
     */
    private $withdrawCashService;

    /**
     * @var UserWithdrawAccountService
     */
    private $withdrawAccountService;

    /**
     * @var UserService
     */
    private $userService;

    protected function init()
    {
        parent::init();
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->withdrawCashService = service('UserWithdrawCash', SERVICE_NAMESPACE);
        $this->withdrawAccountService = service('UserWithdrawAccount', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $keywords = $request->param('keywords', '');
        $params = [];
        if($keywords) {
            if(check_mobile($keywords)) {
                $user = $this->userService->getByMobile($keywords);
            } else {
                $user = $this->userService->getByNick($keywords);
            }
            if($user) {
                $params['user_id'] = $user['id'];
            }
        }
        $res = $this->withdrawCashService->pageListByParams($params, $pageSize);
        if($res['data']) {
            foreach ($res['data'] as &$rs) {
                $rs = $this->withdrawCashService->format($rs);
                $rs['user'] = $this->userService->getByPk($rs['user_id']);
                $rs['user'] = arrayOnly($rs['user'], [
                    'id',
                    'nick',
                    'avatar_url'
                ]);
                $rs['withdraw_account'] = $this->withdrawAccountService->getUserId($rs['user_id']);
            }
        }
        return $this->success($res);
    }

    public function reject(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $remark = $request->param('remark', '');
        $this->withdrawCashService->onReject($id, $remark);
        $item = $this->withdrawCashService->getByPk($id);
        $item = $this->withdrawCashService->format($item);
        $item['user'] = $this->userService->getByPk($item['user_id']);
        $item['user'] = arrayOnly($item['user'], [
            'id',
            'nick',
            'avatar_url'
        ]);
        $item['withdraw_account'] = $this->withdrawAccountService->getUserId($item['user_id']);
        return $this->success($item);
    }

    public function agree(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $this->withdrawCashService->onAgree($id);
        $item = $this->withdrawCashService->getByPk($id);
        $item = $this->withdrawCashService->format($item);
        $item['user'] = $this->userService->getByPk($item['user_id']);
        $item['user'] = arrayOnly($item['user'], [
            'id',
            'nick',
            'avatar_url'
        ]);
        $item['withdraw_account'] = $this->withdrawAccountService->getUserId($item['user_id']);
        return $this->success($item);
    }

    public function pay(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $this->withdrawCashService->onPay($id);
        $item = $this->withdrawCashService->getByPk($id);
        $item = $this->withdrawCashService->format($item);
        $item['user'] = $this->userService->getByPk($item['user_id']);
        $item['user'] = arrayOnly($item['user'], [
            'id',
            'nick',
            'avatar_url'
        ]);
        $item['withdraw_account'] = $this->withdrawAccountService->getUserId($item['user_id']);
        return $this->success($item);
    }
}